package com.xiaoyang.controller;

import org.springframework.http.HttpHeaders;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.MatrixVariable;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author lichenyang
 * @create 2021-07-13 13:40
 */
@RestController
public class ParameterTestController {

    /**
     * 获取请求头信息
     * @param userAgent 获取请求头中的某个属性
     * @param header 获取所有请求头信息
     * @param httpHeaders 获取所有请求头信息
     * @return
     */
    @GetMapping("/getHeadInfo")
    public Map<String, Object> getHeadInfo(@RequestHeader("User-Agent") String userAgent,
                                           @RequestHeader MultiValueMap<String, String> header,
                                           @RequestHeader HttpHeaders httpHeaders) {
        System.out.println(userAgent);
        // 以下2行输出的方式不一样, 其他没什么区别
        // json 格式输出
        System.out.println(header);

        System.out.println(httpHeaders);
        return null;
    }


    /**
     * 获取路径变量, 就是 / / 之间的内容
     * @param id
     * @param pv
     * @return
     */
    @GetMapping("/getPathVariable/{id}/name/{name}")
    public Map<String, Object> getPathVariable(@PathVariable("id") Integer id,
                                               @PathVariable Map<String, String> pv) {
        System.out.println(id);
        System.out.println(pv);
        return null;
    }

    /**
     * 获取 ? 后面的内容, 请求的时候, 所有参数都需要传过来!! 要不然获取不到
     * getRequestParam?age=18&inters=a&b&c"
     *
     * @PathVariable 和 @RequestParam 是不同的 注意区分!!
     *
     * 获取所有请求的内容
     * Map<String, String>
     * MultiValueMap<String, String>
     *
     * @param age age=18, 参数之间 使用 & 连接
     * @param inters inters=a&b&c"
     * @param params
     * @return
     */
    @GetMapping("/getRequestParam")
    public Map<String, Object> getRequestParam(@RequestParam("age") Integer age,
                                               @RequestParam("inters") List<String> inters,
                                               @RequestParam Map<String, String> params,
                                               @RequestParam MultiValueMap<String, String> multiValueMap
                                               ) {
        System.out.println(age);
        System.out.println(inters);
        System.out.println(params);
        System.out.println(multiValueMap);
        return null;
    }


    /**
     * 获取Cookie信息,
     * 获取指定cookie的值
     * 使用 Cookie 接受cookie的所有信息
     *
     * @param _ga
     * @param cookie
     * @return
     */
    @GetMapping("/getCookieValue")
    public Map<String, Object> getCookieValue(@CookieValue("_ga") String _ga,
                                      @CookieValue("_ga") Cookie cookie) {
        HashMap<String, Object> result = new HashMap<>();
        result.put("_ga", _ga);
        System.out.println(_ga);
        return result;
    }

    /**
     * 获取表单提交的所有信息
     * @param content
     * @return
     */
    @PostMapping("/requestBody")
    public Map RequestBody(@RequestBody String content) {
        Map<String, Object> map = new HashMap<>();
        map.put("content", content);
        return map;
    }

    @GetMapping("/goto")
    public String goToPage(HttpServletRequest request){
        System.out.println("进入 gotoPage");
        request.setAttribute("aa", "aa");
        request.setAttribute("bb", "bb");
        return "forward:/success";
    }
    @ResponseBody
    @GetMapping("/success")
    public Map success(@RequestAttribute("aa") String aa,
                       @RequestAttribute("bb") String bb,
                       HttpServletRequest request){
        System.out.println("进入 success");
        String aa1 = (String) request.getAttribute("aa");
        String bb1 = (String) request.getAttribute("bb");
        HashMap<String, String> result = new HashMap<>();
        result.put("aa_HttpServletRequest",aa1);
        result.put("bb_HttpServletRequest",bb1);

        result.put("aa_Attribute", aa);
        result.put("bb_Attribute", bb);
        return result;
    }

    //1、语法： 请求路径：/cars/sell;low=34;brand=byd,audi,yd
    //2、SpringBoot默认是禁用了矩阵变量的功能
    //      手动开启：原理。对于路径的处理。UrlPathHelper进行解析。
    //              removeSemicolonContent（移除分号内容）支持矩阵变量的
    //3、矩阵变量必须有url路径变量才能被解析
    @GetMapping("/cars/{path}")
    public Map carsSell(@MatrixVariable("low") Integer low,
                        @MatrixVariable("brand") List<String> brand,
                        @PathVariable("path") String path) {
        Map<String, Object> map = new HashMap<>();

        map.put("low", low);
        map.put("brand", brand);
        map.put("path", path);
        return map;
    }

    // /boss/1;age=20/2;age=10
    @GetMapping("/boss/{bossId}/{empId}")
    public Map boss(@MatrixVariable(value = "age", pathVar = "bossId") Integer bossAge,
                    @MatrixVariable(value = "age", pathVar = "empId") Integer empAge) {
        Map<String, Object> map = new HashMap<>();

        map.put("bossAge", bossAge);
        map.put("empAge", empAge);
        return map;

    }
}